﻿using System;
using System.Collections;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Xml;

using Aulac.Global.Enums; 
using Aulac.Global.Data; 
using Aulac.Global.Common; 
using Aulac.Global.Xml;
using Aulac.Rsc.Ems; using Aulac.Rsc.Ems.Tags;
using Aulac.Srv.Db.Provider;
using Aulac.Srv.Db.Connect;
using Aulac.Srv.Db.Data.Catalogs;

namespace Aulac.Srv.Db.Data.Ems.Systems
{
	public class UserInfo: DbDataCatalogs
	{
        public UserInfo(IDbAulacConnect p_Db) :
            base(p_Db, TableNames.Sys_UserInfo, 
            tagSystem.Users.UserName, 
            new string[]{tagSystem.Users.UserName}
            )
        {
            mListFieds = new System.Collections.Generic.List<string>(
                 new string[]{tagSystem.Users.UserName, tagSystem.Users.UserPassword, tagSystem.Users.UserGrpID, tagSystem.Users.DateRegister, tagSystem.Users.Note});
        }

        protected override void OnGetListItem(ref DataTable pDataReturn, XmlDataDocument xmlParams)
        {
            sqlQuery = string.Format("SELECT TOP 1 * FROM {0}", TableNames.Sys_UserInfo);
            DbDataProvider.ExecuteReaderDataToTable(mDbCmd, sqlQuery, ref pDataReturn);
        }

        public XmlDataDocument GetUserInfo(XmlDataDocument xmlInDoc)
        {
            try
            {
                string userName = XmlProcess.GetValueParam(xmlInDoc, tagSystem.Users.UserName);
                string UserGroupID = XmlProcess.GetValueParam(xmlInDoc, tagSystem.Users.UserGrpID);
                string sPass = XmlProcess.GetValueParam(xmlInDoc, tagSystem.Users.UserPassword);

                sqlQuery = "SELECT UserGrpID, IsSystem FROM Sys_UserGrp WHERE UserGrpID LIKE ? AND IsSystem LIKE 'true'";
                bool isAdmin = false;
                DbDataReader = DbDataProvider.ExecuteReader(mDbCmd,sqlQuery, UserGroupID);

                if (DbDataReader.Read())
                    isAdmin = true;
                DbDataReader.Close();
                XmlDataDocument xmlDoc = CreateXmlDocument("", "");
                if (isAdmin || (userName.Equals("Admin") && UserGroupID.Equals("Admin")))
                {
                    sqlQuery = string.Format("SELECT * FROM {0} WHERE IsActive = 1", TableNames.Sys_Modules);
                    DbDataReader = DbDataProvider.ExecuteReader(mDbCmd, sqlQuery);
                    XmlElement eHeader = XmlProcess.AddVchHeaderElm(ref xmlDoc);
                    
                    XmlProcess.SetAtrb(ref eHeader, tagSystem.Users.UserGrpID, UserGroupID);
                    XmlProcess.SetAtrb(ref eHeader, tagSystem.Users.UserName, userName);
                    XmlProcess.SetAtrb(ref eHeader, tagSystem.Users.IsSystem, "true");
                    XmlProcess.SetAtrb(ref eHeader, tagSystem.Users.UserPassword, sPass);
                    
                    while (DbDataReader.Read())
                    {
                        XmlElement eLine = XmlProcess.AddVchLineItemElm(xmlDoc);
                        DbDataProvider.GetValuesToXml(DbDataReader, ref eLine, tagSystem.Users.ModuleID, tagSystem.Users.ModuleName);
                        XmlProcess.SetAtrb(ref eLine, tagSystem.Users.IsAccRight, "true");
                        XmlProcess.SetAtrb(ref eLine, tagSystem.Users.IsSystem, "true");
                    }
                    DbDataReader.Close();
                    return xmlDoc;
                }
                else if (userName != null)
                {

                    sqlQuery = "SELECT Sys_Modules.ModuleID, Sys_Modules.ModuleName, Sys_Modules.Location, Sys_Modules.ModuleType, Sys_Modules.IsActive, " +
                      "Sys_UserGrp.UserGrpID, Sys_UserGrp.IsAccRight, Sys_UserGrp.IsAdd, Sys_UserGrp.IsEdit, Sys_UserGrp.IsDelete, Sys_UserGrp.IsPrint, " +
                      "Sys_UserGrp.IsShow, Sys_UserGrp.IsSystem " +
                        "FROM  Sys_Modules INNER JOIN Sys_UserGrp ON Sys_Modules.ModuleID = Sys_UserGrp.ModuleID " +
                        "INNER JOIN Sys_userInfo ON Sys_UserGrp.UserGrpID = Sys_Userinfo.UserGrpID " +
                        "WHERE Sys_UserInfo.UserName LIKE ?";

                    DbDataReader = DbDataProvider.ExecuteReader(mDbCmd, sqlQuery, userName);

                    XmlElement eHeader = null;
                    while (DbDataReader.Read())
                    {
                        if (eHeader == null)
                        {
                            eHeader = XmlProcess.AddVchHeaderElm(ref xmlDoc);
                            DbDataProvider.GetValuesToXml(DbDataReader, ref eHeader,
                                tagSystem.Users.UserName,
                                tagSystem.Users.UserGrpID,
                                tagSystem.Users.UserPassword,
                                tagSystem.Users.DateRegister,
                                tagSystem.Users.IsSystem
                                );
                            XmlProcess.SetAtrb(ref eHeader, tagSystem.Users.UserPassword, sPass);
                        }
                        XmlElement eLine = XmlProcess.AddVchLineItemElm(xmlDoc);
                        DbDataProvider.GetValuesToXml(DbDataReader, ref eLine,
                            tagSystem.Users.UserGrpID,
                            tagSystem.Users.IsAccRight,
                            tagSystem.Users.IsAdd,
                            tagSystem.Users.IsEdit,
                            tagSystem.Users.IsDelete,
                            tagSystem.Users.IsPrint,
                            tagSystem.Users.IsShow,
                            tagSystem.Users.IsSystem,
                            tagSystem.Users.ModuleID,
                            tagSystem.Users.Location
                        );
                    }
                    DbDataReader.Close();
                }
                return xmlDoc;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
       
        public bool IsExistUser(string UserName)
        {
            sqlQuery = "SELECt UserName FROM Sys_UserInfo WHERE UserName LIKE ? ";
            DbDataReader = DbDataProvider.ExecuteReader(mDbCmd, sqlQuery, UserName);
            bool isTrue = false;
            if (DbDataReader.Read())
                isTrue = true;
            DbDataReader.Close();
            return isTrue;
        }
		
	}
}
